package nz.ac.massey.cs.plot;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics2D;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JPanel;

import nz.ac.massey.cs.stats.utils.GroupedData;

public class GroupsScatterPlot extends JPanel{
	private ScatterPlot plot;
	private List<Color> colors;
	private GroupedData data;
	private JComponent legend;
	
	public GroupsScatterPlot() {
		super(new BorderLayout());
		plot = new ScatterPlot(){
			@Override
			public void drawXYPlots(Graphics2D g, List<Double> xs_, List<Double> ys_) {
				int p = 0;
				for(String group : data.getGroups()){
					final List<Double> xs = data.getXs(group);
					final List<Double> ys = data.getYs(group);
					
					int x, y;

					g.setColor(colors.get(p++ % colors.size()));
					g.setFont(getPlotFont());
				
					for (int i = 0; i < xs.size(); i++) {
						x = (int) getScaledX(xs.get(i)) ;
						y = (int) getScaledY(ys.get(i)) + 2;
						g.drawString(data.getSymbol(group).toString(), x, y);
					}
				}
			}
		};
		data = new GroupedData();
		plot.setYmax(Double.MIN_VALUE);
		plot.setXmax(Double.MIN_VALUE);
		plot.setYmin(Double.MAX_VALUE);
		plot.setXmin(Double.MAX_VALUE);
		plot.setShowYGridLines(false);
		
		legend = new JComponent() {
			@Override
			public void paint(java.awt.Graphics g) {
				g.setColor(Color.WHITE);
				g.fillRect(0, 0, getWidth(), getHeight());
				
				g.setColor(Color.BLACK);
				g.setFont(new Font("Arial", Font.BOLD, 15));
				g.drawString("Legend", 15, 15);
				
				g.drawRect(5, 25, getWidth()-20, getHeight()-40);
				
				
				int p = 0;
				for(String group : data.getGroups()){
					String symbol = data.getSymbol(group).toString();
					g.setColor(colors.get(p++ % colors.size()));
					g.setFont(new Font("Arial", Font.BOLD, 13));
					g.drawString(symbol, 15, 30+20*(p));
					g.setColor(Color.BLACK);
					g.setFont(new Font("Arial", Font.PLAIN, 12));
					g.drawString(": "+group, 25, 30+20*(p));
				}
				super.paint(g);
			}
		};
		legend.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
		
		legend.setPreferredSize(new Dimension(150,0));
		
		this.add(plot,BorderLayout.CENTER);
		this.add(legend,BorderLayout.EAST);
		
		this.colors = new ArrayList<Color>(Arrays.asList(
				Color.BLACK,
				new Color(0xC25283), //hot pink
				Color.red,
				new Color(0x461B7E), //purple4
				new Color(0x736AFF), //Light Slate Blue
				new Color(0x3B3131), //dark
				new Color(0x817339), //real gold
				new Color(0x254117), //dark green
				new Color(0x737CA1), //slate blue
				new Color(0x8A4117), //sunset
				new Color(0x0000A0), //dark blue
				Color.green,
				new Color(0x8D38C9), //violet
				Color.blue,
				new Color(0x4C787E), //blue+green
				Color.orange,
				new Color(0xADD8E6), //light blue
				new Color(0xC0C0C0), // silver
				new Color(0x43BFC7), //Turquoise3
				new Color(0xC11B17), //fiber brick
				new Color(0x2554C7), //royal blue
				new Color(0x808000), //olive
				new Color(0x800080), //purple
				new Color(0x82CAFF), //sky blue
				new Color(0x347235), //med. forest green
				new Color(0xFF00FF), //fuchsia
				new Color(0xE4317F), //violet red
				new Color(0x00FFFF), //aqua
				new Color(0xE9AB17), //gold
				new Color(0xA52A2A), //brown
				new Color(0x808080)  //grey
		));
		
	}
	
	public void add(String group, double x, double y){
		data.add(group,x, y);
		plot.setYmax(Math.max(plot.getYmax(), y));
		plot.setYmin(Math.min(plot.getYmin(), y));
		
		plot.setXmax(Math.max(plot.getXmax(), x));
		plot.setXmin(Math.min(plot.getXmin(), x));
	}
	
	public void setSymbols(Map<String,Character> symbols){
		for(Entry<String,Character> entry:symbols.entrySet()){
			data.setSymbol(entry.getKey(), entry.getValue());
		}
	}
	
	public void setColors(List<Color> colors) {
		this.colors = colors;
	}
	
	public void setYmin(double ymin){
		plot.setYmin(ymin);
	}
	public void setYmax(double ymax){
		plot.setYmax(ymax);
	}
	
	public void setXmin(double xmin){
		plot.setXmin(xmin);
	}
	public void setXmax(double xmax){
		plot.setXmax(xmax);
	}
	
	public void setxLabel(String xlabel){
		plot.setxLabel(xlabel);
	}
	public void setyLabel(String ylabel){
		plot.setyLabel(ylabel);
	}

	public void setTitle(String name) {
		plot.setTitle(name);
	}

	public void setTag(String string) {
		plot.setTag(string);
	}
	
}
